package com.qdu.study_app.fragment;

import androidx.fragment.app.ListFragment;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.google.android.material.snackbar.Snackbar;
import com.qdu.study_app.Activity.Activity_Message;
import com.qdu.study_app.Activity.Activity_pub_message;
import com.qdu.study_app.Adapter.MessageListAdapter;
import com.qdu.study_app.R;
import com.qdu.study_app.bean.Notes;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class StudyBoradFragment extends ListFragment {

    private StudyBoradViewModel mViewModel;
    private View view;
    private MessageListAdapter messageListAdapter;

    private Button pub_msg_btn;

    public static final String MSG_TITLE = "com.qdu.study_APP.MSG_TITLE";
    public static final String MSG_CONTENT = "com.qdu.study_APP.MSG_CONTENT";
    public static final String MSG_ID = "com.qdu.study_APP.MSG_ID";
    public static final String MSG_USR_ID = "com.qdu.study_APP.MSG_USER_ID";
    public static final String MSG_CREATE_TIME = "com.qdu.study_APP.MSG_CREATE_TIME";
    public static final String MSG_UPDATE_TIME = "com.qdu.study_APP.MSG_UPDATE_TIME";
    public static StudyBoradFragment newInstance() {
        return new StudyBoradFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.study_borad_fragment, container, false);

        // 给打卡按钮绑定监听事件
        pub_msg_btn = view.findViewById(R.id.pub_msg_btn);
        pub_msg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), Activity_pub_message.class);
                startActivity(i);
            }
        });
        messageListAdapter = new MessageListAdapter(getContext());
        setListAdapter(messageListAdapter);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(StudyBoradViewModel.class);
        // TODO: Use the ViewModel
    }

    /*
    * List每一项的点击事件
    * */
    @Override
    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // 获取列表数据
        Notes message = (Notes) messageListAdapter.getItem(position);
        // 传递数据
        Intent intent = new Intent(getContext(), Activity_Message.class);
        intent.putExtra(MSG_TITLE,message.getTitle());
        intent.putExtra(MSG_CONTENT,message.getContent());
        intent.putExtra(MSG_ID,message.getObjectId());
        intent.putExtra(MSG_USR_ID,message.getUser_id());System.out.println(message.getUser_id()+"getter--userid______________________");
        intent.putExtra(MSG_CREATE_TIME,message.getCreatedAt());
        intent.putExtra(MSG_UPDATE_TIME,message.getUpdatedAt());

        // 页面跳转
        startActivity(intent);

    }

    @NonNull
    @Override
    public ListView getListView() {
        return super.getListView();
    }

    /*
    * 当Fragment重现的时候，重新加载列表
    * */
    @Override
    public void onResume() {
        super.onResume();
        loadMessage();
    //    setLIstViewHeight(getListView());
      //  messageListAdapter.notifyDataSetChanged();
    }

    /*
    * 加载数据函数
    * */
    private void loadMessage(){
        // 获取前先清空
        messageListAdapter.clear();
        // 获取后端的图文消息
        BmobQuery<Notes> bmobArticleBmobQuery = new BmobQuery<>();
        bmobArticleBmobQuery.findObjects(new FindListener<Notes>() {
            @Override
            public void done(List<Notes> list, BmobException e) {
                if(e == null){
                    messageListAdapter.addAll(list);
                    //   setListAdapter();
                    //  Snackbar.make(view,"查询成功"+list.get(0),Snackbar.LENGTH_LONG).show();
                }else {
                    Snackbar.make(view,"查询失败"+e.getMessage(),Snackbar.LENGTH_LONG).show();
                }
            }
        });
    }
    /*
    * 动态设置ListView 的高度
    * */
    public void setLIstViewHeight(ListView listView){
        MessageListAdapter listAdapter = (MessageListAdapter) listView.getAdapter();
        if(listAdapter == null) return;
        int totalHeight = 0;
        for(int i = 0;i < listAdapter.getCount();i++){
            View listItem = listAdapter.getView(i,null,listView);
            listItem.measure(0,0);
            totalHeight+=listItem.getMeasuredHeight();
            }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *  (listAdapter .getCount() - 1));
        listView.setLayoutParams(params);
        System.out.println(totalHeight+"-----------totalheight");


    }

}